package com.ghck.server.dao.provider;

import java.text.MessageFormat;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import com.ghck.server.model.BodypartDict;
import com.ghck.server.model.query.BodypartDictQuery;
/**
 * 身体部位  <br>
 * dict_bodypart <br>
 * v1.0 2016-05-01
 * @see com.ajk.server.dao.BodypartDictMapper
 */
 public class BodypartDictProvider {
 
 	/**
 	* 新增 身体部位
 	*/
 	public String insert(BodypartDict domain){
		StringBuilder sb = new StringBuilder();
		
		sb.append("insert into dict_bodypart(create_time,last_modify_time");
	 	if(domain.getName()!=null){
			sb.append(",name");
		}
	 	if(domain.getParentId()!=null){
			sb.append(",parent_id");
		}
	 	if(domain.getSex()!=null){
			sb.append(",sex");
		}
	 	if(domain.getDescription()!=null){
			sb.append(",description");
		}
	 	if(domain.getInUsed()!=null){
			sb.append(",in_used");
		}
	 	if(domain.getSeq()!=null){
	 		sb.append(",seq");
	 	}
		
		sb.append(") values ( now(),now() ");
		
	 	if(domain.getName()!=null){
			sb.append(",#{name}");
		}
	 	if(domain.getParentId()!=null){
			sb.append(",#{parentId}");
		}
	 	if(domain.getSex()!=null){
			sb.append(",#{sex}");
		}
	 	if(domain.getDescription()!=null){
			sb.append(",#{description}");
		}
	 	if(domain.getInUsed()!=null){
			sb.append(",#{inUsed}");
		}
	 	if(domain.getSeq()!=null){
	 		sb.append(",#{seq}");
	 	}
		sb.append(")");
		
		return sb.toString();
	}

	/**
 	* 更新 身体部位
 	*/
	public String update(BodypartDict domain){
		StringBuilder sb = new StringBuilder();
		
		sb.append("update dict_bodypart set last_modify_time = now() ");
		
		if(domain.getName()!=null){
			sb.append(",name=#{name}");
		}
		if(domain.getParentId()!=null){
			sb.append(",parent_id=#{parentId}");
		}
		if(domain.getSex()!=null){
			sb.append(",sex=#{sex}");
		}
		if(domain.getDescription()!=null){
			sb.append(",description=#{description}");
		}
		if(domain.getInUsed()!=null){
			sb.append(",in_used=#{inUsed}");
		}
	 	if(domain.getSeq()!=null){
	 		sb.append(",seq=#{seq}");
	 	}
		sb.append(" where id=#{id}");
		
		return sb.toString();
	}
	
	public String batchInsert(Map<String, List<BodypartDict>> map){
		StringBuffer sb = new StringBuffer();
		List<BodypartDict> bodypartDicts = map.get("list");
		sb.append("insert into dict_bodypart(create_time,last_modify_time,name,parent_id,sex,description,in_used,seq) values");
	    MessageFormat messageFormat = new MessageFormat("("
	    		+ "now(),now()"
	    		+ ",#'{'list[{0}].name}"
	    		+ ",#'{'list[{0}].parentId}"
	    		+ ",#'{'list[{0}].sex}"
	    		+ ",#'{'list[{0}].description}"
	    		+ ",#'{'list[{0}].inUsed}"
	    		+ ",#'{'list[{0}].seq}"
	    		+")");
	    		
	    if(CollectionUtils.isNotEmpty(bodypartDicts)) {
			sb.append(messageFormat.format(new Integer[]{0}));
			for (int i = 1; i <  bodypartDicts.size(); i++) {
				sb.append(",").append(messageFormat.format(new Integer[]{i}));
			}
		}
		return sb.toString();
	}
	
	/**
 	*通过query 查询 身体部位
 	*/
	public String findByQuery(BodypartDictQuery query){
		StringBuffer sb = new StringBuffer();
		sb.append("select * from dict_bodypart a ");
		
		//TODO: add your logic
		
		if(StringUtils.isEmpty(query.getSortStatment())){
			sb.append(" order by create_time desc ");
		}else{
			sb.append(" ").append(query.getSortStatment());
		}
		if(query.getStart()!=null && query.getLimit()!=null && query.getLimit()!=0){
			sb.append(" limit #{start},#{limit}");
		}else if(query.getLimit()!=null && query.getLimit()!=0){
			sb.append(" limit #{limit}");
		}
		return sb.toString();
	}
	
	public String countByQuery(BodypartDictQuery query){
		StringBuffer sb = new StringBuffer();
		sb.append("select count(1) from dict_bodypart a ");
		return sb.toString();
	}
	
	/**
 	*通过ids 查询 身体部位
 	*/
 	public String findByIds(Map<String, List<Integer>> map){
		StringBuffer sb = new StringBuffer();
		List<Integer> ids = map.get("list");
		sb.append("select * from dict_bodypart where ");
		if(ids==null || ids.isEmpty()){
			sb.append(" 1<2");
		}else{
			if(ids.size()==1){
				sb.append(" id=").append("#{list[0]}");
			}else{
				sb.append(" id in (").append("#{list[0]}");
				for(int i=1; i<ids.size() ; i++){
					sb.append(",").append("#{list[").append(i).append("]}");
				}
				sb.append(")");
			}
		}
		return sb.toString();
	}
	
 }	